package com.xbongbong.pro.rabbitmq.consumer;

import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.product.pojo.dto.AIMessageSendDTO;
import com.xbongbong.pro.rabbitmq.binders.AIConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.AIBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.AIFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * @Description: AI 消费者
 * @Author: jiping.yang
 * @Date: 2022-09-15  11:27
 * @version: v1.0
 **/
@EnableBinding(AIConsumerBinder.class)
public class AIListener {

    private static Logger logger = LoggerFactory.getLogger(AIListener.class);

    @Resource
    private AIFeignClient aiFeignClient;

    @StreamListener(value = AIBinderConstant.AI_INPUT)
    public void aiMessageReception(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type){
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DATA_IMPORT:
                    response = aiFeignClient.importDataSend(message);
                    break;
                case TRAIN_MODEL:
                    response = aiFeignClient.trianDataSend(message);
                    break;
                default:
                    break;
            }
        } catch (RetryableException e) {
            logger.error("AIListener.aiMessageReception RetryableException case by Read timed out");
        } catch (Exception e) {
            logger.error("AIListener.aiMessageReception Exception, type=", e);
        }
    }
}
